package com.xdtech.trans.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.xdtech.common.utils.DateUtil;
import com.xdtech.common.utils.DateUtil.DateStyle;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.sys.aspect.SystemControllerLog;
import com.xdtech.sys.util.SessionContextUtil;
import com.xdtech.sys.vo.UserItem;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.trans.init.TransInit;
import com.xdtech.trans.model.TransOrder;
import com.xdtech.trans.model.TransWebSite;
import com.xdtech.trans.service.PersonService;
import com.xdtech.trans.service.TransOrderScanService;
import com.xdtech.trans.service.TransOrderService;
import com.xdtech.trans.service.TransWebSiteService;
import com.xdtech.trans.util.TransConstants;
import com.xdtech.trans.vo.OrderArriveScanItem;
import com.xdtech.trans.vo.OrderDeliveryScanItem;
import com.xdtech.trans.vo.OrderSendScanItem;
import com.xdtech.trans.vo.OrderSignScanItem;
import com.xdtech.trans.vo.OrderStartScanItem;
import com.xdtech.trans.vo.TransOrderItem;
import com.xdtech.trans.vo.TransOrderScanItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-06-08 23:17:52
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class TransOrderScanController {
	private Log log = LogFactory.getLog(TransOrderScanController.class);
	@Autowired
	private TransOrderScanService transOrderScanService;
	@Autowired
	private TransWebSiteService transWebSiteService;
	@Autowired
	private TransOrderService transOrderService;
	@RequestMapping(value="/transOrderScan.do",params = "transOrderScan")
	public ModelAndView skipTransOrderScan() {
		return new ModelAndView("trans/transOrderScan/transOrderScan_ftl");
	}
	@RequestMapping(value="/transOrderScan.do")
	public ModelAndView scanSkip(HttpServletRequest request,String page) {
		UserItem currentUser = SessionContextUtil.getCurrentUser();
		TransWebSite transWebSite = transWebSiteService.getPersonWebsite(currentUser.getId());
		if (transWebSite!=null) {
			request.setAttribute("personWebsiteId", transWebSite.getId());
		}
		return new ModelAndView("trans/transOrderScan/"+page+"OrderScan_ftl");
	}
	@RequestMapping(value="/transOrderScan.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(TransOrderScanItem item,Pagination pg) {
		Map<String, Object> results =  transOrderScanService.loadPageDataByConditions(pg,item,"findTransOrderScanByCondition");
		return results;
	}
	
	@RequestMapping(value="/transOrderScan.do",params = "editTransOrderScan")
	@SystemControllerLog(description = "编辑")
	public ModelAndView editTransOrderScan(HttpServletRequest request,Long transOrderScanId) {
		if (transOrderScanId!=null) {
			request.setAttribute("transOrderScanItem", transOrderScanService.loadTransOrderScanItem(transOrderScanId));
		}
		return new ModelAndView("trans/transOrderScan/editTransOrderScan_ftl");
	}
	
	@RequestMapping(value="/transOrderScan.do",params = "saveTransOrderScan")
	@ResponseBody
	@SystemControllerLog(description = "保存")
	public ResultMessage saveTransOrderScan(TransOrderScanItem item) {
		ResultMessage r = new ResultMessage();
		if (transOrderScanService.saveOrUpdateTransOrderScan(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/transOrderScan.do",params = "deleteTransOrderScanItems")
	@ResponseBody
	@SystemControllerLog(description = "删除")
	public ResultMessage deleteTransOrderScanItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> transOrderScanIds = new ArrayList<Long>();
			for (String id : tempIds) {
				transOrderScanIds.add(Long.valueOf(id));
			}
			transOrderScanService.deleteTransOrderScanInfo(transOrderScanIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	

	@RequestMapping(value="/transOrderScan.do",params = "sureStartScan")
	@ResponseBody
	@SystemControllerLog(description = "寄件扫描")
	public ResultMessage sureStartScan(TransOrderScanItem item) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(item.getOrderNo())) {
			UserItem currentUser = SessionContextUtil.getCurrentUser();
			item.setScanOperator(currentUser.getName());
			item.setCreator(currentUser.getLoginName());
			item.setScanFrom(TransConstants.FROM_WEB);
			item.setScanType(TransConstants.SCAN_TYPE_START);
			item.setScanTime(DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
			String rs = transOrderScanService.sureStartScan(item);
			if (StringUtils.isNotBlank(rs)) {
				r.setSuccess(false);
				r.setMsg(rs);
			}
		}else {
			r.setSuccess(false);
			r.setMsg("运单号为空");
		}
		return r;
	}
	
	@RequestMapping(value="/transOrderScan.do",params="loadStartScanList")
	@ResponseBody
	public Map<String, Object> loadStartScanList(OrderStartScanItem item,Pagination pg) {
		//设置数据权限过滤
		UserItem currentUser = SessionContextUtil.getCurrentUser();
		if (currentUser.isSuperuser())
		{
			//超级管理员移除，数据过滤
			item.getQueryFields().remove("creator");
		}
		Map<String, Object> results =  transOrderScanService.loadPageDataByConditions(pg,item,"findTransOrderScanByCondition");
		return results;
	}
	
	@RequestMapping(value="/transOrderScan.do",params="loadSendScanList")
	@ResponseBody
	public Map<String, Object> loadSendScanList(OrderSendScanItem item,Pagination pg) {
		//设置数据权限过滤
		UserItem currentUser = SessionContextUtil.getCurrentUser();
		if (currentUser.isSuperuser())
		{
			//超级管理员移除，数据过滤
			item.getQueryFields().remove("creator");
		}
		Map<String, Object> results =  transOrderScanService.loadPageDataByConditions(pg,item,"findTransOrderScanByCondition");
		return results;
	}
	
	@RequestMapping(value="/transOrderScan.do",params="loadArriveScanList")
	@ResponseBody
	public Map<String, Object> loadArriveScanList(OrderArriveScanItem item,Pagination pg) {
		//设置数据权限过滤
		UserItem currentUser = SessionContextUtil.getCurrentUser();
		if (currentUser.isSuperuser())
		{
			//超级管理员移除，数据过滤
			item.getQueryFields().remove("creator");
		}
		Map<String, Object> results =  transOrderScanService.loadPageDataByConditions(pg,item,"findTransOrderScanByCondition");
		return results;
	}
	@RequestMapping(value="/transOrderScan.do",params="loadDeliveryScanList")
	@ResponseBody
	public Map<String, Object> loadDeliveryScanList(OrderDeliveryScanItem item,Pagination pg) {
		//设置数据权限过滤
		UserItem currentUser = SessionContextUtil.getCurrentUser();
		if (currentUser.isSuperuser())
		{
			//超级管理员移除，数据过滤
			item.getQueryFields().remove("creator");
		}
		Map<String, Object> results =  transOrderScanService.loadPageDataByConditions(pg,item,"findTransOrderScanByCondition");
		return results;
	}
	
	@RequestMapping(value="/transOrderScan.do",params="loadSignScanList")
	@ResponseBody
	public Map<String, Object> loadSignScanList(OrderSignScanItem item,Pagination pg) {
		//设置数据权限过滤
		UserItem currentUser = SessionContextUtil.getCurrentUser();
		if (currentUser.isSuperuser())
		{
			//超级管理员移除，数据过滤
			item.getQueryFields().remove("creator");
		}
		Map<String, Object> results =  transOrderScanService.loadPageDataByConditions(pg,item,"findTransOrderScanByCondition");
		return results;
	}
	
	@RequestMapping(value="/transOrderScan.do",params = "sureSendScan")
	@ResponseBody
	@SystemControllerLog(description = "发件扫描")
	public ResultMessage sureSendScan(TransOrderScanItem item) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(item.getOrderNo())) {
			UserItem currentUser = SessionContextUtil.getCurrentUser();
			item.setScanOperator(currentUser.getName());
			item.setCreator(currentUser.getLoginName());
			item.setScanType(TransConstants.SCAN_TYPE_SEND);
			item.setScanFrom(TransConstants.FROM_WEB);
			item.setScanTime(DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
			String rs = transOrderScanService.sureSendScan(item);
			if (StringUtils.isNotBlank(rs)) {
				r.setSuccess(false);
				r.setMsg(rs);
			}
		}else {
			r.setSuccess(false);
			r.setMsg("运单号为空");
		}
		return r;
	}
	
	@RequestMapping(value="/transOrderScan.do",params = "sureArriveScan")
	@ResponseBody
	@SystemControllerLog(description = "到件扫描")
	public ResultMessage sureArriveScan(TransOrderScanItem item) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(item.getOrderNo())) {
			UserItem currentUser = SessionContextUtil.getCurrentUser();
			item.setScanOperator(currentUser.getName());
			item.setCreator(currentUser.getLoginName());
			item.setScanFrom(TransConstants.FROM_WEB);
			item.setScanType(TransConstants.SCAN_TYPE_ARRIVE);
			item.setScanTime(DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
			String rs = transOrderScanService.sureArriveScan(item);
			if (StringUtils.isNotBlank(rs)) {
				r.setSuccess(false);
				r.setMsg(rs);
			}
		}else {
			r.setSuccess(false);
			r.setMsg("运单号为空");
		}
		return r;
	}
	
	@RequestMapping(value="/transOrderScan.do",params = "sureDeliveryScan")
	@ResponseBody
	@SystemControllerLog(description = "派件扫描")
	public ResultMessage sureDeliveryScan(TransOrderScanItem item) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(item.getOrderNo())) {
			UserItem currentUser = SessionContextUtil.getCurrentUser();
			item.setScanOperator(currentUser.getName());
			item.setCreator(currentUser.getLoginName());
			item.setScanFrom(TransConstants.FROM_WEB);
			item.setScanType(TransConstants.SCAN_TYPE_DELIVERY);
			item.setScanTime(DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
			String rs = transOrderScanService.sureDeliveryScan(item);
			if (StringUtils.isNotBlank(rs)) {
				r.setSuccess(false);
				r.setMsg(rs);
			}
		}else {
			r.setSuccess(false);
			r.setMsg("运单号为空");
		}
		return r;
	}
	@RequestMapping(value="/transOrderScan.do",params = "sureSignScan")
	@ResponseBody
	@SystemControllerLog(description = "签收录入")
	public ResultMessage sureSignScan(TransOrderScanItem item) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(item.getOrderNo())) {
			UserItem currentUser = SessionContextUtil.getCurrentUser();
			item.setScanOperator(currentUser.getName());
			item.setCreator(currentUser.getLoginName());
			item.setScanFrom(TransConstants.FROM_WEB);
			item.setScanType(TransConstants.SCAN_TYPE_SIGN);
			item.setScanTime(DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
			String rs = transOrderScanService.sureSignScan(item);
			if (StringUtils.isNotBlank(rs)) {
				r.setSuccess(false);
				r.setMsg(rs);
			}
		}else {
			r.setSuccess(false);
			r.setMsg("运单号为空");
		}
		return r;
	}

	@RequestMapping(value="/transOrderScan.do",params = "loadTransOrderLastestScanInfo")
	@ResponseBody
	public ResultMessage loadTransOrderLastestScanInfo(String orderNo) {
		ResultMessage rs = new ResultMessage();
		if (StringUtils.isNotBlank(orderNo)) {
			TransOrder transOrder = transOrderService.getOrderByNo(orderNo);
			if (transOrder!=null) {
				String orderStatus = transOrder.getOrderStatus();
				
				if (TransConstants.ORDER_STATUS_NO.equals(orderStatus)){
					//审核失败
					rs.setSuccess(false);
					rs.setMsg(String.format("【%s】运单审核失败.", orderNo));
				}else if (TransConstants.ORDER_STATUS_NEW.equals(orderStatus))
				{
					//未审核
					rs.setSuccess(false);
					rs.setMsg(String.format("【%s】运单未审核.", orderNo));
				}
				else {
					TransOrderScanItem transOrderScanItem = new TransOrderScanItem();
					transOrderScanItem = transOrderScanService.loadLastestScanInfoByOrderNo(orderNo);
					if (transOrderScanItem==null) {
						//从未扫描的情况，直接从订单那边将信息复制过来
						TransOrderItem transOrderItem = transOrderService.loadTransOrderItemByOrderNo(orderNo);
						transOrderScanItem = new TransOrderScanItem();
						BeanUtils.copyProperties(transOrderScanItem, transOrderItem);
					}
					UserItem currentUser = SessionContextUtil.getCurrentUser();
					TransWebSite currenttransWebSite = transWebSiteService.getPersonWebsite(currentUser.getId());
					if (currenttransWebSite!=null) {
						transOrderScanItem.setCurrentWebsite(currenttransWebSite.getName());
						transOrderScanItem.setCurrentWebsiteId(currenttransWebSite.getId());
					}
					//回填签收人
					transOrderScanItem.setSignName(transOrderScanItem.getReceiver());
					//默认签收状态
					transOrderScanItem.setSignStatus(TransConstants.SIGN_STATUS_SIGN);
					rs.setObj(transOrderScanItem);
				}
			}else {
				//未审核
				rs.setSuccess(false);
				rs.setMsg(String.format("【%s】运单不存在.", orderNo));
			}
			
		}
		return rs;
	}
	
}
